﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;

namespace TestUI
{
    /// <summary>
    /// ChannelCurve.xaml 的交互逻辑
    /// </summary>
    public partial class ChannelCurve : UserControl
    {

        private const int _bufferLen = 2048;
        private readonly int[] _xCoor = new int[_bufferLen];
        private readonly uint[] _ptVals = new uint[_bufferLen];


        private readonly int[] _xCoorR = new int[2] { 0, 1 };
        private readonly int[] _ptValsR = new int[2] { 0, 0xfff };

        private EnumerableDataSource<int> _dsX;
        private EnumerableDataSource<uint> _dsY;

        private EnumerableDataSource<int> _dsXR;
        private EnumerableDataSource<int> _dsYR;

        public ChannelCurve()
        {
            InitializeComponent();
            XReset();

            _dsX = new EnumerableDataSource<int>(_xCoor);
            _dsY = new EnumerableDataSource<uint>(_ptVals);

            _dsXR = new EnumerableDataSource<int>(_xCoorR);
            _dsYR = new EnumerableDataSource<int>(_ptValsR);
            _dsX.SetXMapping(x => x);
            _dsY.SetYMapping(y => y);

            _dsXR.SetXMapping(x => x);
            _dsYR.SetYMapping(y => y);
            chartPlotter.AddLineGraph(new CompositeDataSource(_dsX, _dsY), System.Windows.Media.Colors.Blue);
            chartPlotter.AddLineGraph(new CompositeDataSource(_dsXR, _dsYR), 0);
            chartPlotter.Legend.Visibility = Visibility.Hidden;
            via.LabelProvider.SetCustomFormatter((val) => string.Format("0x{0:X}", val.Tick));
        }

        public void XReset()
        {
            for (int i = 0; i != _xCoor.Length; i++)
            {
                _xCoor[i] = i;
            }

        }
        public void AddValue(uint[] vals)
        {
            if (vals.Length > _ptVals.Length)
            {
                Console.WriteLine("vals length > ptvals length");
                return;
            }

            var diff = _ptVals.Length - vals.Length;
            for (int i = 0; i < diff; i++)
            {
                _ptVals[i] = _ptVals[i + vals.Length];
                _xCoor[i] += vals.Length;
            }
            for (var i = 0; i < vals.Length; i++)
            {
                _ptVals[diff + i] = vals[i];
                _xCoor[diff + i] += vals.Length;
            }
            _xCoorR[0] += vals.Length;
            _xCoorR[1] += vals.Length;
            System.Threading.Thread.Sleep(1);
            Dispatcher.Invoke(() =>
            {
                _dsXR.RaiseDataChanged();
                _dsX.RaiseDataChanged();
                _dsY.RaiseDataChanged();
                chartPlotter.FitToView();
            });
        }
        public void AddValue(uint val)
        {

            for (int i = _ptVals.Length - 1; i >= 1; i--)
            {
                _ptVals[i] = _ptVals[i - 1];
            }
            _ptVals[0] = val;
            this.Dispatcher.Invoke(_dsY.RaiseDataChanged);
        }
    }
}
